package com.qiangesoft.design.factory.method;

/**
 * 工厂方法模式
 */
public class FactoryMethod {
    public static void main(String[] args) {
        // 奔驰车
        CarFactory benzFactory = new BenzFactory();
        Car benzCar = benzFactory.buildCar();
        benzCar.running();

        // 宝马车
        CarFactory bmwFactory = new BMWFactory();
        Car bmwCar = bmwFactory.buildCar();
        bmwCar.running();

        // 奥迪车
        CarFactory audiFactory = new AudiFactory();
        Car audiCar = audiFactory.buildCar();
        audiCar.running();
    }
}

/**
 * 1.抽象工厂接口
 */
interface CarFactory {
    Car buildCar();
}

/**
 * 2.工厂实现类
 * 奔驰工厂、宝马工厂、奥迪工厂
 */
class BenzFactory implements CarFactory {
    @Override
    public Car buildCar() {
        return new Benz();
    }
}

class BMWFactory implements CarFactory {
    @Override
    public Car buildCar() {
        return new BMW();
    }
}

class AudiFactory implements CarFactory {
    @Override
    public Car buildCar() {
        return new BMW();
    }
}

/**
 * 3.抽象产品接口
 */
interface Car {
    void running();
}

/**
 * 4.产品实现类
 * 奔驰、宝马、奥迪
 */
class Benz implements Car {
    @Override
    public void running() {
        System.out.println("奔驰车在路上跑！");
    }
}

class BMW implements Car {
    @Override
    public void running() {
        System.out.println("宝马车在路上跑！");
    }
}

class Audi implements Car {
    @Override
    public void running() {
        System.out.println("奥迪车在路上跑！");
    }
}